package com.dounine.clouddisk360.parser;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dounine.clouddisk360.exception.CloudDiskException;
import com.dounine.clouddisk360.parser.deserializer.login.Login;
import com.dounine.clouddisk360.parser.deserializer.login.LoginConfig;
import com.dounine.clouddisk360.parser.deserializer.login.LoginConst;
import com.dounine.clouddisk360.parser.deserializer.login.LoginRequestInterceptor;
import com.dounine.clouddisk360.parser.deserializer.login.LoginResponseHandle;
import com.dounine.clouddisk360.parser.deserializer.login.User;
import com.dounine.clouddisk360.pool.PoolingHttpClientConnection;
import com.dounine.clouddisk360.util.MD5Util;

public class LoginParser extends BaseParser<LoginConfig> {

	private static final Logger LOGGER = LoggerFactory.getLogger(LoginParser.class);
	
	public static final String LOGIN_URL = "https://login.360.cn/";

	@Override
	public void initConfig(LoginConfig loginConfig) {
		super.initConfig(loginConfig);//必需调用父类的initConfig方法,或者重写里面所定义的cookieStore路径
	}

	public Login parse(User user) {
		HttpPost request = initRequest(user, super.config);
		Login login = execute(request);
		
		super.setBianaryFilename("userInfo.txt");
		super.setBianaryPath(config.getBasePath()+"user/");
		super.writeObjToDisk(login);
		
		return login;
	}
	
	public HttpPost initRequest(User user, LoginConfig config) {
		HttpPost request = new HttpPost(LOGIN_URL);
		List<NameValuePair> data = new ArrayList<>();
		data.add(new BasicNameValuePair(LoginConst.SRC_KEY, LoginConst.SRC_VAL));
		data.add(new BasicNameValuePair(LoginConst.FROM_KEY, LoginConst.FROM_VAL));
		data.add(new BasicNameValuePair(LoginConst.CHARSET_KEY, LoginConst.SRC_VAL));
		data.add(new BasicNameValuePair(LoginConst.REQUESTSCEMA_KEY, LoginConst.REQUESTSCEMA_VAL));
		data.add(new BasicNameValuePair(LoginConst.O_KEY, LoginConst.O_VAL));
		data.add(new BasicNameValuePair(LoginConst.M_KEY, LoginConst.M_VAL));
		data.add(new BasicNameValuePair(LoginConst.LM_KEY, LoginConst.LM_VAL));
		data.add(new BasicNameValuePair(LoginConst.CAPTFLAG_KEY, LoginConst.CAPTFLAG_VAL));
		data.add(new BasicNameValuePair(LoginConst.RTYPE_KEY, LoginConst.RTYPE_VAL));
		data.add(new BasicNameValuePair(LoginConst.VALIDATELM_KEY, LoginConst.VALIDATELM_VAL));
		data.add(new BasicNameValuePair(LoginConst.ISKEEPALIVE_KEY, LoginConst.ISKEEPALIVE_VAL));
		data.add(new BasicNameValuePair(LoginConst.CAPTCHAAPP_KEY, LoginConst.CAPTCHAAPP_VAL));
		data.add(new BasicNameValuePair(LoginConst.USERNAME_NAME, user.getAccount()));
		data.add(new BasicNameValuePair(LoginConst.TYPE_KEY, LoginConst.TYPE_VAL));
		data.add(new BasicNameValuePair(LoginConst.ACCOUNT_NAME, user.getAccount()));
		data.add(new BasicNameValuePair(LoginConst.PASSWORD_NAME, MD5Util.MD5(user.getPassword())));
		if(StringUtils.isNotBlank(config.getCaptchaValue())){
			data.add(new BasicNameValuePair(LoginConst.CAPTCHA_KEY, config.getCaptchaValue()));
		}else{
			data.add(new BasicNameValuePair(LoginConst.CAPTCHA_KEY, StringUtils.EMPTY));
		}
		if(StringUtils.isNotBlank(config.getToken())){
			data.add(new BasicNameValuePair(LoginConst.TOKEN_NAME, config.getToken()));
		}else{
			throw new CloudDiskException("登录所需token令牌不能为空.");
		}
		data.add(new BasicNameValuePair(LoginConst.PROXY_KEY, LoginConst.PROXY_VAL));
		
		request.setEntity(new UrlEncodedFormEntity(data, Consts.UTF_8));
		
		return request;
	}

	
	public Login execute(HttpPost request) {
		super.httpClient = HttpClients.custom()
				.setConnectionManager(PoolingHttpClientConnection.getInstalce())
				.addInterceptorLast(new LoginRequestInterceptor())
				.setDefaultRequestConfig(cookieRequestConfig())
				.build();
		try {
			return super.httpClient.execute(request,new LoginResponseHandle(this), super.httpClientContext);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
